/**
 * 动作 告警方式
 * @param app
 * @returns {*}
 */
module.exports = app => {
  const mongoose = app.mongoose
  const { Schema } = mongoose
  const { ObjectId } = Schema

  const modelSchema = new Schema({
    name: { type: String, remark: '规则名称', required: true },
    user: { type: ObjectId, required: true, ref: 'user', remark: '创建用户' },
    server: [
      { type: ObjectId, required: true, ref: 'server', remark: '包含服务器' },
    ],
    rule: {
      type: Object,
      required: true,
      remark: '规则信息',
      like: [
        {
          key: 'cpu.avg.0',
          option: '>',
          value: 100,
        },
      ],
    },
    action: { type: ObjectId, ref: 'action', remark: '响应动作' },
    count: { type: Number, default: 0, remark: '触发次数' },
    feedback: { type: String, remark: '负反馈管理指令' },
  }, {
    collections: 'rule',
    timestamps: true,
  })

  modelSchema.index({ name: 1 })

  return mongoose.model('rule', modelSchema)
}